package com.mdd.common.mapper.orders;

import com.mdd.common.core.basics.IBaseMapper;
import com.mdd.common.entity.orders.Orders;
import com.mdd.common.entity.orders.OrdersDishListVo;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper extends IBaseMapper<Orders> {

    @Update("update la_orders set status=2,checkout_time=#{checkoutTime} where desk_id=#{did} and status!=2")
    void updateOrdersStatus(@Param("did") Integer did,@Param("checkoutTime") Long checkoutTime);

    @Update("update la_orders set status=2,checkout_time=#{checkoutTime},amount=#{amount} where id=#{orderId}")
    void updateOrdersStatusAndMeony(@Param("orderId") Integer orderId,@Param("checkoutTime") Long checkoutTime,@Param("amount") BigDecimal amount);

    @Select("select id from la_orders ko where ko.desk_id =#{id} and status <> '2'")
    Integer getOrderId(Integer id);

    @Select("select DISTINCT(s.name) from la_orders ord inner join la_shop_config s on ord.aid = s.aid where ord.id =#{id}")
    String getnickName(Integer id);

    @Select("select DISTINCT a.name,k.user_num  as userNum from la_system_auth_dept a  left join la_orders k on k.desk_id = a.id  where k.id =#{id}")
    Map<String,String> getDiskNameAndNum(Integer id);


    @Select("SELECT id,aid,number,desk_id,user_num,type,status,remark,amount,create_time,checkout_time FROM la_orders WHERE (desk_id = #{id} AND (status = 0 or status = 1)) ORDER BY create_time desc LIMIT 1")
    Orders getNoPayOrder(Integer id);

    @Select("select count(1)+1 from la_orders s where s.aid = (select  a.aid from  la_system_auth_dept a where a.id= #{diskId}) and s.create_time > #{time}")
    Integer getCurrentNum(@Param("diskId") Integer diskId,@Param("time") Long time);


    @Select("SELECT sum(t.quantity) as number,ar.title AS NAME,ar.image as image ,ar.summary as amount FROM la_orders_dish t INNER JOIN la_article ar ON t.dish_id = ar.id INNER JOIN la_orders ord ON ord.id = t.order_id WHERE ord.id = (select id from la_orders a where a.desk_id = #{diskId} and `status` =1 ) and t.order_flag =2 GROUP BY t.dish_id")
    List<OrdersDishListVo> getorderedList(Integer diskId);


    @Delete("delete from la_orders  where id =#{id}")
    void deleteOrderId(Integer id);

    /**
     *
     * @param id
     */
    @Select("SELECT id from la_system_auth_admin b where b.dept_ids like CONCAT('%', #{id}, '%') or b.id = #{id}")
    List<Integer> getCurrentAidChindId(String id);

}
